﻿cms.Dialogs = function() {

    function _showOverlay() {
        if (document.getElementById("modalDialogOverlay"))
            return;
        var div = document.createElement("div");
        div.id = "modalDialogOverlay";
        div.style.display = 'block';
        div.style.zIndex = 1000;
        div.style.backgroundColor = "#073f62";
        div.style.width = 100 + "%";
        div.style.height = 100 + "%";
        div.style.top = 0 + "px";
        div.style.left = 0 + "px";
        div.style.position = "fixed";
        div.style.overflow = "hidden";
        var opacity = 30;
        var opaque = (opacity / 100);
        div.style.opacity = opaque;
        div.style.MozOpacity = opaque;
        div.style.filter = 'alpha(opacity=' + opacity + ')';
        document.body.insertBefore(div, null);
    }


    function _hideOverlay() {
        var div = document.getElementById("modalDialogOverlay");
        if (div) {
            document.body.removeChild(div);
        }
    }


    function _createPopup() {
        var modalDialog = document.createElement("div");
        modalDialog.id = "modalDialog";
        document.body.insertBefore(modalDialog, null);
        modalDialog.style.width = 626 + "px";
        modalDialog.style.position = "fixed";
        modalDialog.style.left = (document.documentElement.clientWidth - modalDialog.offsetWidth) / 2 + "px";
        modalDialog.style.top = (document.documentElement.clientHeight - modalDialog.offsetHeight) / 2 * 0.5 + "px";
        modalDialog.style.zIndex = 1001;
        var imgTop = document.createElement("div");
        imgTop.className = "modalDialogTopImg";
        modalDialog.appendChild(imgTop);
        var modalContentContainer = document.createElement("div");
        modalContentContainer.className = "modalDialogContentContainer";
        var modalContent = document.createElement("div");
        modalContent.className = "modalDialogContent";
        modalDialog.appendChild(modalContentContainer);
        modalContentContainer.appendChild(modalContent);
        var imgBottom = document.createElement("div");
        imgBottom.className = "modalDialogBottomImg";
        modalDialog.appendChild(imgBottom);
        return modalContent;
    }


    function _confirmLinkButton(linkbutton, onclickHandler, header, message, okText, cancelText, hideDialogOnOk, name) {
        var hrefHandler = new Function(linkbutton.href.substr(11)); // remove "javascript:" from the href attribute
        callback = function(status) {
            if (typeof onclickHandler == "function")
                onclickHandler(status);
            if (status == "ok")
                hrefHandler();
        }
        return _confirm(callback, header, message, okText, cancelText, hideDialogOnOk, name);
    }

    var _currentConfirmCallback;
    var _hideDialogOnOk;
    var _importedDiv;

    // Parameters:
    // callback - a function being called upon OK/Cancel with parameter ok/cancel
    // header - dialog header
    // message - the dialog message, can be html
    // okText - the text for the OK button
    // cancelText - the text for the Cancel button
    // hideDialogOnOk - if the dialog is used with a regular linkbutton the dialog disappears when the page reloads,
    //                  but on an AJAX request this can be set to true instead
    // name - optionally used in conjunction with message to display e.g. the name of an object to be deleted
    // importDivID - if the dialog is used to display a form instead of a regular message, use this to state the ID of the div containing the form
    function _confirm(callback, header, message, okText, cancelText, hideDialogOnOk, name, importDivID) {
        _showOverlay();
        if (name != null && name != "")
            message = message.replace(/\[\[name\]\]/gi, name);
        _currentConfirmCallback = callback;
        _hideDialogOnOk = hideDialogOnOk;
        var modalContent = _createPopup();
        var h1 = document.createElement("h1");
        h1.innerHTML = header;
        modalContent.appendChild(h1);
        var divMessage = document.createElement("div");
        divMessage.id = "modalDialogMessage";
        _importedDiv = null;
        if (message != null)
            divMessage.innerHTML = message;
        else {
            if (importDivID != null && importDivID != "undefined" && importDivID != "") {
                _importedDiv = document.getElementById(importDivID);
                while (_importedDiv && _importedDiv.childNodes.length > 0) {
                    divMessage.appendChild(_importedDiv.firstChild);
                }
            }
        }
        divMessage.className = "modalDialogMessage";
        modalContent.appendChild(divMessage);
        var btnOK = document.createElement("a");
        btnOK.className = "whiteButton";
        btnOK.innerHTML = '<span><span><img src="Admin/Img/icons/accept.png" /><span>' + okText + '</span></span></span>';
        btnOK.href = "javascript:void(0);";
        iLib.Dom.AddListener(btnOK, "click", function(e) {
            if (_importedDiv != null) {
                _restoreImportedDiv();
            }
            iLib.Dom.RemoveListener(document, "keydown", _captureEsc);
            if (hideDialogOnOk) {
                var div = document.getElementById("modalDialog");
                if (div)
                    document.body.removeChild(div);
                _hideOverlay();
            }
            if (typeof callback == "function")
                callback("ok");
        });
        iLib.Dom.AddListener(document, "keydown", _captureEnter);
        modalContent.appendChild(btnOK);
        var btnCancel = document.createElement("a");
        btnCancel.className = "whiteButton";
        btnCancel.innerHTML = '<span><span><img src="Admin/Img/icons/delete.png" /><span>' + cancelText + '</span></span></span>';
        btnCancel.href = "javascript:void(0);";
        iLib.Dom.AddListener(btnCancel, "click", function(e) {
            if (_importedDiv != null) {
                _restoreImportedDiv();
            }
            var div = document.getElementById("modalDialog");
            if (div)
                document.body.removeChild(div);
            iLib.Dom.RemoveListener(document, "keydown", _captureEsc);
            _hideOverlay();
            if (typeof callback == "function")
                callback("cancel");
        });
        modalContent.appendChild(btnCancel);
        iLib.Dom.AddListener(document, "keydown", _captureEsc);
        iLib.Dom.AddListener(modalContent, "keydown", function(e) {
            var key = iLib.Dom.DetectKey();
            if (key == 9 || key == 13 || key == 32) {
                iLib.Dom.PreventBubble(e);
                return true;
            }
        });
        btnOK.focus();
        btnOK.blur();
        return false;
    }

    function _captureEnter(e) {
        var key = iLib.Dom.DetectKey(e);
        if (key == 13) {
            if (_importedDiv != null) {
                _restoreImportedDiv();
            }
            if (_hideDialogOnOk) {
                var div = document.getElementById("modalDialog");
                if (div)
                    document.body.removeChild(div);
                _hideOverlay();
            }
            if (typeof _currentConfirmCallback == "function")
                _currentConfirmCallback("ok");
            iLib.Dom.RemoveListener(document, "keydown", _captureEsc);
            iLib.Dom.RemoveListener(document, "keydown", _captureEnter);
        }
        return true;
    }

    function _captureEsc(e) {
        if (iLib.Dom.DetectKey(e) == 27) {
            if (_importedDiv != null) {
                _restoreImportedDiv();
            }
            var div = document.getElementById("modalDialog");
            if (div)
                document.body.removeChild(div);
            _hideOverlay();
            iLib.Dom.RemoveListener(document, "keydown", _captureEsc);
            if (typeof _currentConfirmCallback == "function")
                _currentConfirmCallback("cancel");
        }
        return true;
    }

    function _cancelConfirmMessage() {
        var div = document.getElementById("modalDialog");
        if (div)
            document.body.removeChild(div);
        iLib.Dom.RemoveListener(document, "keydown", _captureEsc);
        _hideOverlay();
    }

    function _restoreImportedDiv() {
        var divMessage = document.getElementById("modalDialogMessage");
        while (divMessage && divMessage.childNodes.length > 0) {
            _importedDiv.appendChild(divMessage.firstChild);
        }
    }

    function _renderAlerts() {
        if (_alerts.length > 0) {
            var a = _alerts.shift();
            a();
        }
    }


    var _alerts = [];
    var _currentAlertCallback;

    function _renderAlert(callback, header, message, okText) {
        _showOverlay();
        var modalContent = _createPopup();
        var h1 = document.createElement("h1");
        h1.innerHTML = header;
        modalContent.appendChild(h1);
        var divMessage = document.createElement("div");
        divMessage.innerHTML = message;
        modalContent.appendChild(divMessage);
        var btnOK = document.createElement("a");
        btnOK.className = "whiteButton";
        btnOK.innerHTML = '<span><span><img src="Admin/Img/icons/accept.png" /><span>' + okText + '</span></span></span>';
        btnOK.href = "javascript:void(0);";
        iLib.Dom.AddListener(btnOK, "click", function() {
            if (typeof callback == "function")
                callback();
            var div = document.getElementById("modalDialog");
            if (div)
                document.body.removeChild(div);
            _hideOverlay();
            _renderAlerts();
        });
        _currentAlertCallback = callback;
        iLib.Dom.AddListener(document, "keydown", _alertKeyDownHandler);
        modalContent.appendChild(btnOK);
        btnOK.focus();
        btnOK.blur();
    }

    function _alertKeyDownHandler(e) {
        var key = iLib.Dom.DetectKey(e);
        if (key == 13 || key == 27) {
            if (typeof _currentAlertCallback == "function")
                _currentAlertCallback();
            var div = document.getElementById("modalDialog");
            if (div)
                document.body.removeChild(div);
            if (_alerts.length == 0)
                _hideOverlay();
            iLib.Dom.RemoveListener(document, "keydown", _alertKeyDownHandler);
            window.setTimeout(function() { _renderAlerts(); }, 0);
            return false;
        }
    }

    function _registerAlert(callback, header, message, okText) {
        _alerts[_alerts.length] = function() {
            _renderAlert(callback, header, message, okText)
        };
    }

    function _alert(callback, header, message, okText) {
        _renderAlert(callback, header, message, okText)
    }

    function _customAlert(callback, header, okText, importDivID) {
        _showOverlay();
        _hideDialogOnOk = true;
        var modalContent = _createPopup();
        var h1 = document.createElement("h1");
        h1.innerHTML = header;
        modalContent.appendChild(h1);
        var divMessage = document.createElement("div");
        divMessage.id = "modalDialogMessage";
        _importedDiv = null;
        if (importDivID != null && importDivID != "undefined" && importDivID != "") {
            _importedDiv = document.getElementById(importDivID);
            while (_importedDiv && _importedDiv.childNodes.length > 0) {
                divMessage.appendChild(_importedDiv.firstChild);
            }
        }
        divMessage.className = "modalDialogMessage";
        modalContent.appendChild(divMessage);
        var btnOK = document.createElement("a");
        btnOK.className = "whiteButton";
        btnOK.innerHTML = '<span><span><img src="Admin/Img/icons/accept.png" /><span>' + okText + '</span></span></span>';
        btnOK.href = "javascript:void(0);";
        iLib.Dom.AddListener(btnOK, "click", function(e) {
            if (_importedDiv != null) {
                _restoreImportedDiv();
            }
            iLib.Dom.RemoveListener(document, "keydown", _captureEsc);
            var div = document.getElementById("modalDialog");
            if (div)
                document.body.removeChild(div);
            _hideOverlay();
            if (typeof callback == "function")
                callback("ok");
        });
        iLib.Dom.AddListener(document, "keydown", _captureEnter);
        modalContent.appendChild(btnOK);
        iLib.Dom.AddListener(document, "keydown", _captureEsc);
        iLib.Dom.AddListener(modalContent, "keydown", function(e) {
            var key = iLib.Dom.DetectKey();
            if (key == 9 || key == 13 || key == 32) {
                iLib.Dom.PreventBubble(e);
                return true;
            }
        });
        var modalDialog = document.getElementById("modalDialog");
        modalDialog.style.top = (document.documentElement.clientHeight - modalDialog.offsetHeight) / 2 * 0.5 + "px"; 
        btnOK.focus();
        btnOK.blur();
        return false;
    }

    return {
        Alert: _alert,
        RegisterAlert: _registerAlert,
        Confirm: _confirm,
        ConfirmLinkButton: _confirmLinkButton,
        RenderAlerts: _renderAlerts,
        CaptureEnter: _captureEnter,
        CustomAlert: _customAlert,
        ShowOverlay: _showOverlay
    }

} ();
